﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    [Description("在采购申请单中选择物料时自动携带其对应的父项物料编码")]
    [HotUpdate]
    public class RequisitionInBOMEdit : AbstractBillPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if (e.Field.Key == "FMaterialId")
            {
                var value = (DynamicObject)this.Model.GetValue("FMATERIALID", e.Row);//物料数据包
                if (value != null) //判断是否为空
                {
                    string FID = value["ID"].ToString();//物料内码
                    DynamicObject SrcBillNo = this.View.Model.GetValue("SrcBillNo") as DynamicObject;//源单编码
                    if (SrcBillNo == null || SrcBillNo.ToString().IsNullOrEmpty())//判断源单编码是否为空
                    {
                        DynamicObject basicData = value["MaterialGroup"] as DynamicObject;//物料分组
                        if (basicData != null) //判断物料分组是否为空
                        {
                            string number = basicData["Number"].ToString();
                            if (number == "04.01" || number == "04.02" || number == "04.04" || number == "04.09" || number== "04.48")
                            {
                                string sql = string.Format(@"select a.FMATERIALID,c.FBASEUNITID from T_ENG_BOM a
                                                            left join T_ENG_BOMCHILD b on a.FID=b.FID
                                                            left join T_BD_MATERIALBASE c on a.FMATERIALID = c.FMATERIALID
                                                            where b.FMATERIALID='" + FID + "' and FDOCUMENTSTATUS='C' order by FCREATEDATE desc");
                                DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                                if (sql_toStirng.Rows.Count > 0)
                                {
                                    string FMATERIALID = sql_toStirng.Rows[0]["FMATERIALID"].ToString();//父项物料内码
                                    string FUNITID = sql_toStirng.Rows[0]["FBASEUNITID"].ToString();//父项物料基本单位
                                    this.View.Model.SetValue("F_DEV_PARENTITEMMATERIAL", FMATERIALID, e.Row);
                                    this.View.Model.SetValue("F_DEV_Parentunit", FUNITID, e.Row);
                                }
                                else
                                {
                                    this.View.Model.SetValue("F_DEV_PARENTITEMMATERIAL", 0, e.Row);
                                    this.View.Model.SetValue("F_DEV_Parentunit", 0, e.Row);
                                }
                            }
                            else
                            {
                                this.View.Model.SetValue("F_DEV_PARENTITEMMATERIAL", 0, e.Row);
                                this.View.Model.SetValue("F_DEV_Parentunit", 0, e.Row);
                            }
                        }

                    }
                }
                else
                {
                    this.View.Model.SetValue("F_DEV_PARENTITEMMATERIAL", 0, e.Row);
                    this.View.Model.SetValue("F_DEV_Parentunit", 0, e.Row);
                }

            }
        }
    }
}
